"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容M* create tablespace语法
Case Name   : 创建tablespace指定无写权限datafile
Create At   : 2022.09.6
@zou_jialiang0501129488
Description :
    1、创建兼容b库并查询插件是否加载成功
    2、创建无写权限datafile
    3、创建tablespace后指定无写权限datafile
    4、清理环境
Expect      :
    1、成功，插件查询成功
    2、成功
    3、成功
    4、清理成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.user_node = Node('PrimaryDbUser')
        self.root_node = Node('PrimaryRoot')
        self.sta1_root_node = Node('Standby1Root')
        self.sta2_root_node = Node('Standby2Root')
        self.constant = Constant()
        self.datafile = os.path.join(os.path.dirname(macro.DB_INSTANCE_PATH),
                                     'tbsp_file_case3')
        self.database = 'db_tbsp_case3'
        self.tbspname = 'tbsp_case3'

    def test_create_tablespqce(self):
        text = '-----step1:创建兼容b库并查询插件;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.database};" \
                  f"create database {self.database} " \
                  f"dbcompatibility ='B' encoding ='UTF8';"
        self.log.info(sql_cmd)
        sql_res = Primary_SH.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)
        sql_cmd1 = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd1)
        sql_res1 = Primary_SH.execut_db_sql(sql_cmd1, dbname=self.database)
        self.log.info(sql_res1)
        self.assertIn('dolphin', sql_res1, '查询失败')

        text = '-----step2:创建无写权限的datafile目录;expect:成功-----'
        self.log.info(text)
        create_cmd = f'rm -rf {self.datafile};mkdir -p {self.datafile};' \
                     f'chmod 500 {self.datafile};' \
                     f'ls {os.path.dirname(macro.DB_INSTANCE_PATH)};'
        self.log.info(create_cmd)
        create_res = self.common.get_sh_result(self.user_node, create_cmd)
        self.assertIn('tbsp_file_case3', create_res, '文件创建失败')

        text = '-----step3:创建tablespace;expect:成功-----'
        self.log.info(text)
        sql_cmd3 = f"drop tablespace if exists {self.tbspname};" \
                   f"create tablespace {self.tbspname} add " \
                   f"datafile \'{self.datafile}\';"
        self.log.info(sql_cmd3)
        sql_res3 = Primary_SH.execut_db_sql(sql_cmd3, dbname=self.database)
        self.log.info(sql_res3)
        self.assertIn('DROP TABLESPACE', sql_res3, '删除失败')
        self.assertIn('CREATE TABLESPACE', sql_res3, '创建失败')

    def tearDown(self):
        text = '-----step4:清理环境;expect:成功-----'
        self.log.info(text)
        sql_cmd4 = f"drop tablespace if exists {self.tbspname};" \
                   f"drop database if exists {self.database};"
        self.log.info(sql_cmd4)
        sql_res4 = Primary_SH.execut_db_sql(sql_cmd4)
        self.log.info(sql_res4)
        del_cmd = f'rm -rf {self.datafile};' \
                  f'if [ -d {self.datafile} ]; ' \
                  f'then echo "exists"; else echo "not exists"; fi'
        self.log.info(del_cmd)
        del_res = self.common.get_sh_result(self.root_node, del_cmd)
        del_res1 = self.common.get_sh_result(self.sta1_root_node, del_cmd)
        del_res2 = self.common.get_sh_result(self.sta2_root_node, del_cmd)
        self.assertIn('DROP TABLESPACE', sql_res4, '删除失败')
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res4,
                      '执行失败' + text)
        self.assertIn('not exists', del_res, '文件删除失败')
        self.assertIn('not exists', del_res1, '文件删除失败')
        self.assertIn('not exists', del_res2, '文件删除失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
